//
//  TFNetWorkSelectView.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/4/21.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFNetWorkSelectView: UIView , UITableViewDelegate,UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "TFNetWorkCell", for: indexPath) as! TFNetWorkCell
        cell.selectionStyle = .none
        
        cell.tf_width = self.view.frame.size.width;
         let model = self.dataSource[indexPath.row] as! TFNetWorkModel
        cell.titleLab.text = NSString.init(format: "线路%ld", indexPath.row+1) as String

        cell.model = model;
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        for i in 0..<self.dataSource.count {
            let model = self.dataSource[i] as! TFNetWorkModel
            model.isSelect = false
        }
    
         let model = self.dataSource[indexPath.row] as! TFNetWorkModel
         model.isSelect = true
        

        
        if self.delegate != nil && (self.delegate?.responds(to: #selector(TFNetWorkSelectViewDelegate.setNetwork(_:selectedSegmentIndex:))))!{
            
          
            
            self.delegate?.setNetwork(model.key as NSString , selectedSegmentIndex: indexPath.row)
        }
        
        
        self.tableview.reloadData()
       
        
    }
    /**关闭*/
    @objc public var closeBtn = UIButton()
    /**title*/
    @objc public var titleLab = UILabel()

    /**tipsLab*/
    @objc public var tipsLab = UILabel()
 
    @objc public var tableview = UITableView()
    @objc public var dataSource = NSMutableArray()

    @objc public var isShow:Bool = false
    
    @objc public weak var delegate : TFNetWorkSelectViewDelegate?

    
    let view = UIButton()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.isHidden = true
        self.view.backgroundColor = UIColor.white
        self.addSubview(self.view)
        
        self.closeBtn.setImage(UIImage.init(named: "HTLive.bundle/close_gray"), for: .normal)
       
        self.view.addSubview(self.closeBtn)
        

        self.titleLab.text = "网络选择";
        self.titleLab.font = UIFont.PingFang(size: 19)
        self.titleLab.textAlignment = .center
        self.titleLab.textColor = UIColor(red:20/255.0 , green: 23/255.0, blue: 26/255.0, alpha: 1)
        self.view.addSubview(self.titleLab)
        

        self.tipsLab.text = "95%的情况下，自动选择的线路最好";
        self.tipsLab.font = UIFont.PingFang(size: 15)
        self.tipsLab.textColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1)
        self.tipsLab.textAlignment = .center
        self.view.addSubview(self.tipsLab)
        

        self.tableview.backgroundColor = UIColor.clear
        self.tableview.delegate = self;
        self.tableview.dataSource = self;
        self.view.addSubview(self.tableview)
        
        self.tableview.register(TFNetWorkCell.self, forCellReuseIdentifier: "TFNetWorkCell")
        self.tableview.rowHeight = 55;
        self.tableview.separatorStyle = .none;
    }
    
    func showView(_ isShow:Bool)
    {
        if (self.superview != nil) {
            self.superview?.addSubview(self)
        }
        //横屏模式
        if self.frame.width > self.frame.height {
            
            self.backgroundColor = UIColor.clear
            
            let viewW:CGFloat = 375
            var viewX = self.frame.width
            
            if isShow {
                viewX = self.frame.width - viewW
            }
            
            if isShow {
                self.view.frame = CGRect(x: self.frame.width, y: 0, width: viewW, height: self.frame.height)
               
            }
            self.layoutSubviewsUI()
          
            self.isHidden = false
            UIView.animate(withDuration: 0.5, delay: 0.0, options: UIView.AnimationOptions.allowAnimatedContent) {
                self.view.frame = CGRect(x: viewX, y: 0, width: viewW, height: self.frame.height)
            } completion: { Bool in
                
                if isShow == false {
                    self.isHidden = true
                }
            }
        }else
        {
            
            self.backgroundColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.5)

            
            let viewH:CGFloat = 246
            var viewY = self.frame.height
            
            if isShow {
                viewY = self.frame.height - viewH
            }
            
            if isShow {
                self.view.frame = CGRect(x: 0, y: self.frame.height, width: self.frame.width, height: viewH)
               
            }
            
            self.layoutSubviewsUI()
            self.isHidden = false
            UIView.animate(withDuration: 0.5, delay: 0.0, options: UIView.AnimationOptions.allowAnimatedContent) {
                self.view.frame = CGRect(x: 0, y: viewY, width: self.frame.width, height: viewH)
            } completion: { Bool in
                
                if isShow == false {
                    self.isHidden = true
                }
            }
            
        }
        
    
        
    }
     func layoutSubviewsUI() {
  
        self.closeBtn.frame = CGRect(x: self.view.frame.size.width-40, y: 10, width: 32, height: 32)
   
        self.titleLab.frame = CGRect(x: 0, y: 20, width: self.view.frame.size.width, height: 20)
      
        self.tipsLab.frame = CGRect(x: 0, y: self.titleLab.frame.maxY+10, width: self.view.frame.size.width, height: 20)
       
        self.tableview.frame = CGRect(x: 0, y: self.tipsLab.frame.maxY+10, width: self.view.frame.size.width, height: self.view.frame.size.height-self.tipsLab.frame.maxY-10)
         
         self.tableview.reloadData()
         
    }
  @objc public func updataNetWork(model:TFLiveLineManager)

    {
        self.dataSource.removeAllObjects()
 
        let arr = model.data as NSMutableArray
        if (arr.count == 0) {
            return;
        }
        for i in 0..<arr.count {

            let data = arr[i] as! TFLiveLineData
            let lineList = data.lineList as NSArray
            let livemodel = lineList[0] as! TFLiveLine
            
            let  netmodel = TFNetWorkModel()
            
            netmodel.name =  livemodel.name;
            netmodel.key = livemodel.key;
            if (i==0) {
                netmodel.isSelect = true;
            }
            self.dataSource.add(netmodel)
         
        }
        self.tableview.reloadData()

    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.showView(false)
    }

}
@objc protocol TFNetWorkSelectViewDelegate : NSObjectProtocol {
    /**选择网络服务商*/
    func setNetwork(_ operatorID : NSString , selectedSegmentIndex:NSInteger)
    
   

}
